﻿using System;
using System.Web;

namespace msBsc.WebUi
{
    public partial class wfLogin : msBsc.Cls.BPage
    {
        //msBsc.AppLogic.Cfg.UserInfo oUserInfo = new AppLogic.Cfg.UserInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strRetPath = HttpContext.Current.Request.Url.ToString();
                int item111 = strRetPath.ToLower().IndexOf("returnurl=");
                if (item111 != -1)
                {
                    strRetPath = strRetPath.Substring(item111 + 10, strRetPath.Length - item111 - 10);
                    if (HttpContext.Current.Request.ApplicationPath != "/")
                        strRetPath = msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase.Substring(0, msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase.ToLower().LastIndexOf(HttpContext.Current.Request.ApplicationPath.ToLower())) + strRetPath;
                    //this.ExecJScript("window.location.href='" + msBsc.AppLogic.Cfg.HttpTools.EzUrlEncode(strRetPath) + "';");

                    if (strRetPath.ToLower().IndexOf("home") != -1)
                    {
                        strRetPath = "";
                    }
                    this.ExecJScript("$('#txtRetUrl').val('" + strRetPath + "');");

                    //msBsc.AppLogic.Cfg.UserInfo oUserInfo = new AppLogic.Cfg.UserInfo();
                    if (msBsc.SvRef.UserInfo.Online)
                    {
                        this.Response.Redirect(strRetPath);
                    }
                }
                else if (this.Request.UrlReferrer != null)
                {
                    if (this.Request.UrlReferrer.PathAndQuery.ToLower().IndexOf("home") == -1)
                    {
                        this.ExecJScript("$('#txtRetUrl').val('" + this.Request.UrlReferrer.PathAndQuery + "');");
                    }
                    //msBsc.AppLogic.Cfg.UserInfo oUserInfo = new AppLogic.Cfg.UserInfo();
                    if (msBsc.SvRef.UserInfo.Online)
                    {
                        this.Response.Redirect(this.Request.UrlReferrer.PathAndQuery);
                    }
                }
                else
                {
                    //msBsc.AppLogic.Cfg.UserInfo oUserInfo = new AppLogic.Cfg.UserInfo();
                    if (msBsc.SvRef.UserInfo.Online)
                    {
                        this.ExecJScript("RetToUrl();");
                        //this.Response.Redirect("/");
                    }
                }




                //Response.Expires = 0;
                //Response.CacheControl = "no-cache";
                 
                 
                //Response.Expires = 0;
                //Response.CacheControl = "no-cache";

                //safeCodeVerify_1.Visible = false;
                //TxtVerify_1.Visible = false;
                //txtRetUrl.Value = this.ToPageRegisterReturnUrl();


                //string strRetPath = HttpContext.Current.Request.Url.ToString();
                //if (strRetPath.IndexOf("ReturnUrl=") != -1)
                //{
                //    strRetPath = strRetPath.Substring(strRetPath.IndexOf("ReturnUrl=") + 10, strRetPath.Length - strRetPath.IndexOf("ReturnUrl=") - 10);
                //    if (HttpContext.Current.Request.ApplicationPath != "/")
                //        strRetPath = msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase.Substring(0, msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase.ToLower().LastIndexOf(HttpContext.Current.Request.ApplicationPath.ToLower())) + strRetPath;
                //    //this.ExecJScript("window.location.href='" + msBsc.AppLogic.Cfg.HttpTools.EzUrlEncode(strRetPath) + "';");

                //    txtRetUrl.Value = strRetPath;
                //}
                //if (txtRetUrl.Value != string.Empty)
                //{
                //    if (txtRetUrl.Value.ToUpper().IndexOf("wfThome".ToUpper()) != -1)
                //    {
                //        txtRetUrl.Value = "";
                //    }
                //    else if (msBsc.SvRef.UserInfo.Online)
                //    {
                //        Response.Redirect(txtRetUrl.Value);
                //    }
                //}
                ////Alert(txtRetUrl.Value);
                ////Alert((HttpContext.Current.User == null).ToString() + "-ddd");
                ////if (HttpContext.Current.User != null)
                ////{
                ////    Alert(HttpContext.Current.User.Identity.IsAuthenticated.ToString());
                ////    Alert(HttpContext.Current.User.Identity.Name);
                ////}
                //if (msBsc.SvRef.UserInfo.Online)
                //{
                //    this.ExecJScript("RetToUrl();");
                //}
            }
        }


        public override void InitialByParam()
        {
            if (GetParamHtValue("ACTION") == "mLogin")
            {  
                //if (string.IsNullOrEmpty(this.GetParamHtValue("PLEDGE_PROJECT_CDE")))
                //{
                //    DataSet dsBsePartner = objBsePartner.SelectVehProjectByUserCde(this.GetParamHtValue("USER_NME"));
                //    if (dsBsePartner.Tables[0].Rows.Count > 0)
                //    {
                //        sOperatorpledgeProject = dsBsePartner.Tables[0].Rows[0]["PLEDGE_PROJECT_CDE"].ToString();
                //    }
                //}
                //else
                //{
                //    sOperatorpledgeProject = this.GetParamHtValue("PLEDGE_PROJECT_CDE");
                //}

                string strRet = msBsc.SvRef.UserInfo.Login(this.GetParamHtValue("USER_NME"), this.GetParamHtValue("USER_PASS") );
                if (strRet == "OK")
                {
                    //DialogSkin = null;
                    //string strRetPath = HttpContext.Current.Request.Url.ToString();
                    //strRetPath = strRetPath.Substring(strRetPath.IndexOf("ReturnUrl=") + 10, strRetPath.Length - strRetPath.IndexOf("ReturnUrl=") - 10);
                    //if (HttpContext.Current.Request.ApplicationPath != "/")
                    //    strRetPath = msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase.Substring(0, msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase.LastIndexOf(HttpContext.Current.Request.ApplicationPath)) + strRetPath;
                    //this.ExecJScript("window.location.href='" + msBsc.AppLogic.Cfg.HttpTools.EzUrlEncode(this.GetParamHtValue("txtRetUrl")) + "';");

                    //Alert((HttpContext.Current.User == null).ToString()+"-ddd");
                    //Alert(HttpContext.Current.User.Identity.IsAuthenticated.ToString());
                    //Alert(HttpContext.Current.User.Identity.Name);



                    if (msBsc.SvRef.UserInfo.OperType == "6")
                    {
                        this.ExecJScript("if($('#txtRetUrl').val()==''){$('#txtRetUrl').val('/h_ShopFwdHome.htm');}");
                    }
                    else if (msBsc.SvRef.UserInfo.OperType == "5")
                    {
                        this.ExecJScript("if($('#txtRetUrl').val()==''){$('#txtRetUrl').val('/h_ShopPromoHome.htm');}");
                    }
                    else if (msBsc.SvRef.UserInfo.OperType == "4")
                    {
                        this.ExecJScript("if($('#txtRetUrl').val()==''){$('#txtRetUrl').val('/h_FwdHome.htm');}");
                    }
                    else if (msBsc.SvRef.UserInfo.OperType == "3")
                    {
                        this.ExecJScript("if($('#txtRetUrl').val()==''){$('#txtRetUrl').val('/h_PromoHome.htm');}");
                    }
                    else if (msBsc.SvRef.UserInfo.OperType == "0")
                    {
                        this.ExecJScript("if($('#txtRetUrl').val()==''){$('#txtRetUrl').val('/s_THome.htm');}");
                    }

                    this.ExecJScript("LS.set('CURR_LOGIN_USR_NME','" + this.GetParamHtValue("USER_NME") + "');");
                    if (this.GetParamHtValue("SAVE_PSW") == "1")
                    {
                        this.ExecJScript("LS.set('CURR_LOGIN_USR_PSW','" + this.GetParamHtValue("USER_PASS") + "');");
                    }
                    this.ExecJScript("LS.set('CURR_LOGIN_USR_SAVE','" + this.GetParamHtValue("SAVE_PSW") + "');");
                    this.ExecJScript("RetToUrl();");
                    

                    //FormsAuthentication.RedirectFromLoginPage("*", false);
                    //Response.Redirect(msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase + "wftHome.aspx");
                }
                else
                {
                    SetContrlValue("dddd", strRet);
                }
            } 

            base.InitialByParam();
        }

         
    }
}